//
//  SAPersonInfoVC.m
//  saasRetail
//
//  Created by 黄志刚 on 2021/8/18.
//

#import "SAPersonInfoVC.h"
#import "UILabel+NSMutableAttributedString.h"
#import "HDDatePickerView.h"
#import <AipOcrSdk/AipOcrSdk.h>
#import "SAPayCommonUploadAPI.h"
#import "SAPayRegisterDetailModel.h"
#import "SAPayRegisterLegalPersonAPI.h"
#import "SAPayRegisterDetailAPI.h"
#import "SABaseInfoVC.h"
#import "QRCodePermissions.h"
#import "NSString+FintCommon.h"
#import "SAConsultationWechatVC.h"

@interface SAPersonInfoVC ()<HTTPAPIDelegate,UITextFieldDelegate>

@property (nonatomic, strong) UIButton *nextBtn;
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) NSDateFormatter *dateFormatter;

@property (nonatomic, strong) NSString *startDate;
@property (nonatomic, strong) NSString *endDate;
@property (nonatomic, strong) UIButton *startBtn;
@property (nonatomic, strong) UIButton *endBtn;

@property (nonatomic, strong) UITextField *certificationNameTf;
@property (nonatomic, strong) UITextField *certificationNumTf;
@property (nonatomic, strong) UIImageView *positiveImageV;
@property (nonatomic, strong) UIImageView *reverseImageV;

@property (nonatomic, strong) SAPayCommonUploadAPI *payCommonUploadAPI;//身份证前面照片
@property (nonatomic, strong) SAPayCommonUploadAPI *payCommonUploadAPI2;//身份证反面照片

@property (nonatomic, strong) NSString *frontImageUrl;
@property (nonatomic, strong) NSString *backImageUrl;
@property (nonatomic, strong) NSString *idcardFrontPic;
@property (nonatomic, strong) NSString *idcardBackPic;

@property (nonatomic, strong) SAPayRegisterDetailModel *model;
@property (nonatomic, strong) SAPayRegisterLegalPersonAPI *payRegisterLegalPersonAPI;
@property (nonatomic, strong) SAPayRegisterDetailAPI *payRegisterDetailAPI;

@property (nonatomic, strong) UIImageView *cameraImageV;
@property (nonatomic, strong) UIImageView *cameraImageV2;
@property (nonatomic, strong) NSString *cardNum;
@property (nonatomic, strong) UIButton *wechatBtn;

@end

@implementation SAPersonInfoVC

-(instancetype)initWithModel:(SAPayRegisterDetailModel *)model {
    if (self = [super init]) {
        self.model = model;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

#pragma mark - HTTPAPIDelegate
- (void)fetchData:(id)data fromServerSuccessedWithAPI:(HTTPAPI *)api {
    if (self.payCommonUploadAPI == api) {
        [HDHubView dismiss];
        self.frontImageUrl = data[@"data"][@"imgURL"];
        self.idcardFrontPic = data[@"data"][@"fileId"];
        [self showNext];
    }else if (self.payCommonUploadAPI2 == api) {
        [HDHubView dismiss];
        self.backImageUrl = data[@"data"][@"imgURL"];
        self.idcardBackPic = data[@"data"][@"fileId"];
        [self showNext];
    }else if (self.payRegisterLegalPersonAPI == api) {
        [self.payRegisterDetailAPI sendRequestToServerWithActivityViewAndMask:POST];
    }else if (self.payRegisterDetailAPI == api) {
        self.model = [SAPayRegisterDetailModel mj_objectWithKeyValues:data[@"data"]];
        SABaseInfoVC *pushVC = [[SABaseInfoVC alloc] initWithModel:self.model];
        [self.navigationController pushViewController:pushVC animated:YES];
    }
}

- (void)fetchDataFromServerFailedWithError:(NSError *)error API:(HTTPAPI *)api {
    if (self.payCommonUploadAPI == api) {
        [HDHubView dismiss];
        self.idcardFrontPic = @"";
    }else if (self.payCommonUploadAPI2 == api) {
        [HDHubView dismiss];
        self.idcardBackPic = @"";
    }
}

#pragma mark - UITextFieldDelegate
- (void)textFieldEditChanged:(UITextField*)textField {
    NSString *newStr = [textField.text noEmoji];
    if (textField.text.length != newStr.length) {
        textField.text = newStr;
    }
    [self showNext];
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (string.length == 0) {
        return YES;
    }
    
//    if ([string containsEmoji] == YES) {
//        return N[self.receiveQueryStatusAPI sendRequestToServerWithType:POST];
//    }

    if (self.certificationNumTf == textField) {
        if (textField.text.length > 17) {
            return NO;
        }
        if([string containsString:@"x"] || [string containsString:@"X"]) {
            textField.text = [NSString stringWithFormat:@"%@%@",textField.text,string];
            return NO;
        }
        return [self validateNumber:string];
    }
    return YES;
}

- (BOOL)validateNumber:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@".0123456789"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}

#pragma mark - event
-(void)clickedNextBtn {
    if (self.certificationNumTf.text.length > 18) {
        [Prompt popPromptViewWithMsg:@"身份证号码必须小于18位" duration:2];
        return;
    }
    if ([self.model.merchantDetailVO.auditStatus isEqualToString:@"0"] || [self.model.merchantDetailVO.auditStatus isEqualToString:@"1"]) {
        SABaseInfoVC *pushVC = [[SABaseInfoVC alloc] initWithModel:self.model];
        [self.navigationController pushViewController:pushVC animated:YES];
        return;
    }
    self.payRegisterLegalPersonAPI.legalName = self.certificationNameTf.text;
    self.payRegisterLegalPersonAPI.idCardEnd = self.endDate;
    self.payRegisterLegalPersonAPI.idcardBackPic = self.idcardBackPic;
    self.payRegisterLegalPersonAPI.merchantId = [SAValueManger shareInStance].userInfo.merchantId;
    NSString *idCardNo = [self.certificationNumTf.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    self.payRegisterLegalPersonAPI.idCardNo = idCardNo;
    self.payRegisterLegalPersonAPI.idcardFrontPic = self.idcardFrontPic;
    self.payRegisterLegalPersonAPI.idCardStart = self.startDate;
    [self.payRegisterLegalPersonAPI sendRequestToServerWithActivityViewAndMask:POST];
}

- (void)clickStartDateBtn:(UIButton *)btn {
    WeakSelf
    HDDatePickerView *pickerView = [[HDDatePickerView alloc] initWithSlectedDate:self.startDate];
    [pickerView show];
    pickerView.selectDateBlock = ^(NSString *selectedDate) {
        NSDate *date1 = [self.dateFormatter dateFromString:selectedDate];
        NSDate *date2 = [self.dateFormatter dateFromString:weakSelf.endDate];
        if ([date1 compare:date2] != NSOrderedAscending && weakSelf.endDate.length > 0) {
            [Prompt popPromptViewWithMsg:@"开始日期必须小于截止日期" duration:2];
        }else {
            weakSelf.startDate = selectedDate;
            NSString *date = [selectedDate stringByReplacingOccurrencesOfString:@"-" withString:@"."];
            [weakSelf.startBtn setTitle:date forState:UIControlStateNormal];
            [weakSelf.startBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
            [weakSelf showNext];
        }
    };
}

- (void)clickEndDateBtn:(UIButton *)btn {
    WeakSelf
    HDDatePickerView *pickerView = [[HDDatePickerView alloc] initWithSlectedDate:self.endDate];
    [pickerView show];
    pickerView.selectDateBlock = ^(NSString *selectedDate) {
        NSDate *date1 = [self.dateFormatter dateFromString:selectedDate];
        NSDate *date2 = [self.dateFormatter dateFromString:weakSelf.startDate];
        if ([date1 compare:date2] != NSOrderedDescending && weakSelf.startDate.length > 0) {
            [Prompt popPromptViewWithMsg:@"截止日期必须大于开始日期" duration:2];
        }else {
            weakSelf.endDate = selectedDate;
            NSString *date = [selectedDate stringByReplacingOccurrencesOfString:@"-" withString:@"."];
            [weakSelf.endBtn setTitle:date forState:UIControlStateNormal];
            [weakSelf.endBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
            [weakSelf showNext];
        }
    };
}

- (void)idcardOCROnlineFront {
    
    if (![QRCodePermissions allowCamera]) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请开启摄像头权限" message:@"请在\"设置 - 隐私 - 相机\"选项中，允许访问您的相机" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        }];
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
        }];
        [alert addAction:action];
        [alert addAction:action2];
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
        return;
    }
    
    __weak typeof(self) weakSelf = self;
    [[AipOcrService shardService] clearCache];
    UIViewController * vc =
    [AipCaptureCardVC ViewControllerWithCardType:CardTypeIdCardFont
                                 andImageHandler:^(UIImage *image) {
        [[AipOcrService shardService] detectIdCardFrontFromImage:image
                                                     withOptions:nil
                                                  successHandler: ^(id result){
            
            NSString *name = @"";
            NSString *Id = @"";
            if(result[@"words_result"]){
                if([result[@"words_result"] isKindOfClass:[NSDictionary class]]){
                    name = result[@"words_result"][@"姓名"][@"words"];
                    Id = result[@"words_result"][@"公民身份号码"][@"words"];
                }
            }
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                if (name.length > 0){
                    weakSelf.certificationNameTf.text = name;
                }
                if (Id.length > 0){
                    weakSelf.certificationNumTf.text = Id;
                }
                if (image) {
                    weakSelf.nextBtn.frame = CGRectMake(kScaleNum(70), kScaleNum(StatusHeight + 380), kScaleNum(235), kScaleNum(35));
                    weakSelf.wechatBtn.frame = CGRectMake((kScreenWidth - kScaleNum(150))/2, kScaleNum(StatusHeight + 415), kScaleNum(150), kScaleNum(35));
                    weakSelf.bgView.hidden = NO;
                    weakSelf.positiveImageV.image = image;
                    [weakSelf.payCommonUploadAPI sendServerImageData:image];
                    [HDHubView loadingMessage:@"正在上传图片,请稍等..."];
                }
                [weakSelf showNext];
                [weakSelf dismissViewControllerAnimated:YES completion:nil];
            }];
        }
                                                     failHandler:^(NSError *error){
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                if (image) {
                    weakSelf.nextBtn.frame = CGRectMake(kScaleNum(70), kScaleNum(StatusHeight + 380), kScaleNum(235), kScaleNum(35));
                    weakSelf.wechatBtn.frame = CGRectMake((kScreenWidth - kScaleNum(150))/2, kScaleNum(StatusHeight + 415), kScaleNum(150), kScaleNum(35));
                    weakSelf.bgView.hidden = NO;
                    [weakSelf.payCommonUploadAPI sendServerImageData:image];
                    [HDHubView loadingMessage:@"正在上传图片,请稍等..."];
                }
                [weakSelf showNext];
                [weakSelf dismissViewControllerAnimated:YES completion:nil];
            }];
        }];
    }];
    vc.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:vc animated:YES completion:nil];
}

- (void)idcardOCROnlineBack{
    
    if (![QRCodePermissions allowCamera]) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请开启摄像头权限" message:@"请在\"设置 - 隐私 - 相机\"选项中，允许访问您的相机" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        }];
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
        }];
        [alert addAction:action];
        [alert addAction:action2];
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
        return;
    }
    
    __weak typeof(self) weakSelf = self;
    [[AipOcrService shardService] clearCache];
    UIViewController * vc =
    [AipCaptureCardVC ViewControllerWithCardType:CardTypeIdCardBack
                                 andImageHandler:^(UIImage *image) {
        [[AipOcrService shardService] detectIdCardBackFromImage:image
                                                    withOptions:nil
                                                 successHandler: ^(id result){
            
            NSString *startDate = @"";
            NSString *endDate = @"";
            if(result[@"words_result"]){
                if([result[@"words_result"] isKindOfClass:[NSDictionary class]]){
                    startDate = result[@"words_result"][@"签发日期"][@"words"];
                    endDate = result[@"words_result"][@"失效日期"][@"words"];
                }
            }
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                if (image) {
                    weakSelf.nextBtn.frame = CGRectMake(kScaleNum(70), kScaleNum(StatusHeight + 380), kScaleNum(235), kScaleNum(35));
                    weakSelf.wechatBtn.frame = CGRectMake((kScreenWidth - kScaleNum(150))/2, kScaleNum(StatusHeight + 415), kScaleNum(150), kScaleNum(35));
                    weakSelf.bgView.hidden = NO;
                    
                    if (startDate.length == 8) {
                        weakSelf.startDate = [weakSelf formatDate:startDate];
                        NSString *newDate = [weakSelf.startDate stringByReplacingOccurrencesOfString:@"-" withString:@"."];
                        [weakSelf.startBtn setTitle:newDate forState:UIControlStateNormal];
                        [weakSelf.startBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
                    }
                    if (endDate.length == 8){
                        weakSelf.endDate = [weakSelf formatDate:endDate];
                        NSString *newDate2 = [weakSelf.endDate stringByReplacingOccurrencesOfString:@"-" withString:@"."];
                        [weakSelf.endBtn setTitle:newDate2 forState:UIControlStateNormal];
                        [weakSelf.endBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
                    }
                    weakSelf.reverseImageV.image = image;
                    [weakSelf.payCommonUploadAPI2 sendServerImageData:image];
                    [HDHubView loadingMessage:@"正在上传图片,请稍等..."];
                }
                [weakSelf showNext];
                [weakSelf dismissViewControllerAnimated:YES completion:nil];
            }];
        }
                                                    failHandler:^(NSError *error){
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                if (image) {
                    weakSelf.nextBtn.frame = CGRectMake(kScaleNum(70), kScaleNum(StatusHeight + 380), kScaleNum(235), kScaleNum(35));
                    weakSelf.wechatBtn.frame = CGRectMake((kScreenWidth - kScaleNum(150))/2, kScaleNum(StatusHeight + 415), kScaleNum(150), kScaleNum(35));
                    weakSelf.bgView.hidden = NO;
                    
                    weakSelf.reverseImageV.image = image;
                    [weakSelf.payCommonUploadAPI2 sendServerImageData:image];
                    [HDHubView loadingMessage:@"正在上传图片,请稍等..."];
                }
                [weakSelf showNext];
                [weakSelf dismissViewControllerAnimated:YES completion:nil];
            }];
        }];
    }];
    vc.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:vc animated:YES completion:nil];
}

-(NSString *)formatDate:(NSString *)date {
    NSString *newStr = [date substringToIndex:6];
    NSString *newStr2 = [NSString stringWithFormat:@"%@-%@-%@",[newStr substringToIndex:4],[newStr substringFromIndex:4],[date substringFromIndex:6]];
    return newStr2;
}

-(void)showNext {
    BOOL isShow = YES;
    if (self.idcardFrontPic.length == 0) {
        isShow = NO;
    }
    if (self.idcardBackPic.length == 0) {
        isShow = NO;
    }
    if (self.certificationNameTf.text.length == 0) {
        isShow = NO;
    }
    if (self.certificationNumTf.text.length == 0) {
        isShow = NO;
    }
    if (self.startDate.length == 0) {
        isShow = NO;
    }
    if (self.endDate.length == 0) {
        isShow = NO;
    }
    if (isShow == YES) {
        self.nextBtn.userInteractionEnabled = YES;
        [self.nextBtn setBackgroundImage:[UIImage imageNamed:@"button_blue_bg"] forState:UIControlStateNormal];
    }else {
        self.nextBtn.userInteractionEnabled = NO;
        [self.nextBtn setBackgroundImage:[UIImage imageNamed:@"button_gray_bg"] forState:UIControlStateNormal];
    }
}

-(void)clickedWechatBtn {
    SAConsultationWechatVC *pushVC = [SAConsultationWechatVC new];
    [self.navigationController pushViewController:pushVC animated:YES];
}

-(void)initData {
    if ([self.model.merchantDetailVO.legalInfoStatus isEqualToString:@"1"]) {
        self.nextBtn.frame = CGRectMake(kScaleNum(70), kScaleNum(StatusHeight + 380), kScaleNum(235), kScaleNum(35));
        self.wechatBtn.frame = CGRectMake((kScreenWidth - kScaleNum(150))/2, kScaleNum(StatusHeight + 415), kScaleNum(150), kScaleNum(35));
        self.bgView.hidden = NO;
        
        self.frontImageUrl = self.model.merchantLegalPersonVO.idcardFrontPicUrl;
        self.backImageUrl = self.model.merchantLegalPersonVO.idcardBackPicUrl;;
        self.idcardFrontPic = self.model.merchantLegalPersonVO.idcardFrontPic;
        self.idcardBackPic = self.model.merchantLegalPersonVO.idcardBackPic;
        
        [self.positiveImageV sd_setImageWithURL:[NSURL URLWithString:self.frontImageUrl] placeholderImage:[UIImage imageNamed:@"pay_card_positive"]];
        [self.reverseImageV sd_setImageWithURL:[NSURL URLWithString:self.backImageUrl] placeholderImage:[UIImage imageNamed:@"pay_card_reverse"]];
        self.certificationNameTf.text = self.model.merchantLegalPersonVO.legalName;
        self.certificationNumTf.text = self.model.merchantLegalPersonVO.idCardNo;
        self.startDate = self.model.merchantLegalPersonVO.idCardStart;
        self.endDate = self.model.merchantLegalPersonVO.idCardEnd;
        NSString *newStartDate = [self.startDate stringByReplacingOccurrencesOfString:@"-" withString:@"."];
        [self.startBtn setTitle:newStartDate forState:UIControlStateNormal];
        NSString *newEndDate = [self.endDate stringByReplacingOccurrencesOfString:@"-" withString:@"."];
        [self.endBtn setTitle:newEndDate forState:UIControlStateNormal];
        [self.startBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
        [self.endBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
        
        if (self.model.merchantLegalPersonVO.idCardNo.length > 16) {
            self.cardNum = [self.model.merchantLegalPersonVO.idCardNo substringFromIndex:17];
        }
        [self showNext];
    }
    
    if ([self.model.merchantDetailVO.auditStatus isEqualToString:@"0"] || [self.model.merchantDetailVO.auditStatus isEqualToString:@"1"]) {
        self.positiveImageV.userInteractionEnabled = NO;
        self.reverseImageV.userInteractionEnabled = NO;
        self.certificationNameTf.userInteractionEnabled = NO;
        self.certificationNumTf.userInteractionEnabled = NO;
        self.startBtn.userInteractionEnabled = NO;
        self.endBtn.userInteractionEnabled = NO;
        self.cameraImageV.hidden = YES;
        self.cameraImageV2.hidden = YES;
    }
}

#pragma mark - initUI
-(void)initUI {
    [self setNavigationItemBackBBIAndTitle:@"身份资料"];
    self.view.backgroundColor = kLineColor;
    
    UIImageView *positiveImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pay_card_positive"]];
    positiveImageV.frame = SARectMake(20, StatusHeight + 30, 160, 111);
    positiveImageV.userInteractionEnabled = YES;
    positiveImageV.contentMode = UIViewContentModeScaleAspectFit;
    [self.view addSubview:positiveImageV];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(idcardOCROnlineFront)];
    [positiveImageV addGestureRecognizer:tap];
    self.positiveImageV = positiveImageV;
    
    UIImageView *cameraImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pay_camera"]];
    cameraImageV.frame = SARectMake(20, StatusHeight + 30, 160, 111);
    cameraImageV.userInteractionEnabled = NO;
    cameraImageV.contentMode = UIViewContentModeCenter;
    [self.view addSubview:cameraImageV];
    self.cameraImageV = cameraImageV;
    
    UILabel *positiveTipLab = [[UILabel alloc] initWithFrame:SARectMake(20, StatusHeight + 150, 160, 15)];
    positiveTipLab.text = @"上传身份证正面";
    positiveTipLab.textColor = kBlackColor;
    positiveTipLab.font = kFont(13);
    positiveTipLab.numberOfLines = 0;
    positiveTipLab.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:positiveTipLab];
    
    UIImageView *reverseImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pay_card_reverse"]];
    reverseImageV.frame = SARectMake(195, StatusHeight + 30, 160, 111);
    reverseImageV.userInteractionEnabled = YES;
    [self.view addSubview:reverseImageV];
    reverseImageV.contentMode = UIViewContentModeScaleAspectFit;
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(idcardOCROnlineBack)];
    [reverseImageV addGestureRecognizer:tap2];
    self.reverseImageV = reverseImageV;
    
    UIImageView *cameraImageV2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pay_camera"]];
    cameraImageV2.frame = SARectMake(195, StatusHeight + 30, 160, 111);
    cameraImageV2.userInteractionEnabled = NO;
    cameraImageV2.contentMode = UIViewContentModeCenter;
    [self.view addSubview:cameraImageV2];
    self.cameraImageV2 = cameraImageV2;
    
    UILabel *reverseTipLab = [[UILabel alloc] initWithFrame:SARectMake(195, StatusHeight + 150, 160, 15)];
    reverseTipLab.text = @"上传身份证反面";
    reverseTipLab.textColor = kBlackColor;
    reverseTipLab.font = kFont(13);
    reverseTipLab.numberOfLines = 0;
    reverseTipLab.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:reverseTipLab];
    
    UILabel *tipLab = [[UILabel alloc] initWithFrame:SARectMake(45, StatusHeight + 217, kBaseScreenWidth - 90, 34)];
    tipLab.text = @"根据乐刷、微信、支付宝开通关于收款服务的要求，请上传商户身份证照片，完成身份认证";
    tipLab.textColor = kBuleColor;
    tipLab.font = kFont(14);
    tipLab.numberOfLines = 0;
    tipLab.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:tipLab];
    
    UIView *bgView = [[UIView alloc] initWithFrame:SARectMake(0, StatusHeight + 188, kBaseScreenWidth, 170)];
    bgView.backgroundColor = kLineColor;
    [self.view addSubview:bgView];
    bgView.hidden = YES;
    self.bgView = bgView;
    
    UILabel *tipLab2 = [[UILabel alloc] initWithFrame:SARectMake(40, 0, kBaseScreenWidth - 80, 15)];
    tipLab2.text = @"*请核对以下识别的信息，若识别有误，请及时修改";
    tipLab2.textColor = kLightgGrayColor;
    tipLab2.font = kFont(13);
    tipLab2.numberOfLines = 0;
    tipLab2.textAlignment = NSTextAlignmentCenter;
    [bgView addSubview:tipLab2];
    [tipLab2 setAttributedStringWithSubString:@"*" color:[UIColor redColor]];
    
    UILabel *titleLab1 = [[UILabel alloc] initWithFrame:SARectMake(0, 29, 80, 35)];
    titleLab1.text = @"姓名";
    titleLab1.textColor = kBlackColor;
    titleLab1.font = kFont(15);
    titleLab1.numberOfLines = 0;
    titleLab1.textAlignment = NSTextAlignmentRight;
    [bgView addSubview:titleLab1];
    
    UILabel *titleLab2 = [[UILabel alloc] initWithFrame:SARectMake(0, 78, 80, 35)];
    titleLab2.text = @"身份证";
    titleLab2.textColor = kBlackColor;
    titleLab2.font = kFont(15);
    titleLab2.numberOfLines = 0;
    titleLab2.textAlignment = NSTextAlignmentRight;
    [bgView addSubview:titleLab2];
    
    UILabel *titleLab3 = [[UILabel alloc] initWithFrame:SARectMake(0, 127, 80, 35)];
    titleLab3.text = @"有效期";
    titleLab3.textColor = kBlackColor;
    titleLab3.font = kFont(15);
    titleLab3.numberOfLines = 0;
    titleLab3.textAlignment = NSTextAlignmentRight;
    [bgView addSubview:titleLab3];
    
    UITextField *certificationNameTf = [[UITextField alloc] initWithFrame:SARectMake(106, 29, 233, 35)];
    certificationNameTf.placeholder = @"请输入姓名";
    certificationNameTf.layer.borderColor = kColor(200, 200, 200, 1).CGColor;
    certificationNameTf.layer.borderWidth = 1;
    UIView *nameleft = [[UIView alloc] initWithFrame:SARectMake(0, 0, 10, 35)];
    certificationNameTf.leftView = nameleft;
    certificationNameTf.leftViewMode = UITextFieldViewModeAlways;
    certificationNameTf.clearButtonMode = UITextFieldViewModeWhileEditing;
    [bgView addSubview:certificationNameTf];
    certificationNameTf.delegate = self;
    [certificationNameTf addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    self.certificationNameTf = certificationNameTf;
    
    UITextField *certificationNumTf = [[UITextField alloc] initWithFrame:SARectMake(106, 78, 233, 35)];
    certificationNumTf.placeholder = @"请输入身份证号码";
    certificationNumTf.layer.borderColor = kColor(200, 200, 200, 1).CGColor;
    certificationNumTf.layer.borderWidth = 1;
    UIView *cardleft = [[UIView alloc] initWithFrame:SARectMake(0, 0, 10, 35)];
    certificationNumTf.leftView = cardleft;
    certificationNumTf.leftViewMode = UITextFieldViewModeAlways;
    certificationNumTf.clearButtonMode = UITextFieldViewModeWhileEditing;
    [bgView addSubview:certificationNumTf];
    certificationNumTf.delegate = self;
    [certificationNumTf addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    self.certificationNumTf = certificationNumTf;
    certificationNumTf.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    
    UIButton *startBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    startBtn.frame = CGRectMake(kScaleNum(106), kScaleNum(127), kScaleNum(95), kScaleNum(35));
    [startBtn setTitleColor:kColor(190, 190, 190, 1) forState:UIControlStateNormal];
    startBtn.layer.borderColor = kColor(200, 200, 200, 1).CGColor;
    [startBtn setTitle:@"请选择" forState:UIControlStateNormal];
    startBtn.layer.borderWidth = 1;
    startBtn.titleLabel.font = kFont(15);
    [startBtn addTarget:self action:@selector(clickStartDateBtn:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:startBtn];
    self.startBtn = startBtn;
    
    UILabel *titleLab4 = [[UILabel alloc] initWithFrame:SARectMake(201, 127, 43, 35)];
    titleLab4.text = @"至";
    titleLab4.textColor = kBlackColor;
    titleLab4.font = kFont(15);
    titleLab4.textAlignment = NSTextAlignmentCenter;
    [bgView addSubview:titleLab4];
    
    UIButton *endBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    endBtn.frame = CGRectMake(kScaleNum(244), kScaleNum(127), kScaleNum(95), kScaleNum(35));
    [endBtn setTitleColor:kColor(190, 190, 190, 1) forState:UIControlStateNormal];
    endBtn.layer.borderColor = kColor(200, 200, 200, 1).CGColor;
    [endBtn setTitle:@"请选择" forState:UIControlStateNormal];
    endBtn.layer.borderWidth = 1;
    endBtn.titleLabel.font = kFont(15);
    [endBtn addTarget:self action:@selector(clickEndDateBtn:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:endBtn];
    self.endBtn = endBtn;
    
    UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [nextBtn setBackgroundImage:[UIImage imageNamed:@"button_gray_bg"] forState:UIControlStateNormal];
    nextBtn.frame = CGRectMake(kScaleNum(70), kScaleNum(StatusHeight + 294), kScaleNum(235), kScaleNum(35));
    [nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
    nextBtn.titleLabel.font = kFont(16);
    nextBtn.userInteractionEnabled = NO;
    [nextBtn addTarget:self action:@selector(clickedNextBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:nextBtn];
    self.nextBtn = nextBtn;
    
    UIButton *wechatBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    wechatBtn.frame = CGRectMake((kScreenWidth - kScaleNum(150))/2, kScaleNum(StatusHeight + 329), kScaleNum(150), kScaleNum(35));
    [wechatBtn setTitle:@"添加客服咨询>" forState:UIControlStateNormal];
    [wechatBtn setTitleColor:kBuleColor forState:UIControlStateNormal];
    wechatBtn.titleLabel.font = kFont(13);
    [wechatBtn addTarget:self action:@selector(clickedWechatBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:wechatBtn];
    self.wechatBtn = wechatBtn;
}

-(NSDateFormatter *)dateFormatter {
    if (!_dateFormatter) {
        _dateFormatter = [NSDateFormatter new];
        [_dateFormatter setDateFormat:@"yyyy-MM-dd"];
    }
    return _dateFormatter;
}

-(SAPayCommonUploadAPI *)payCommonUploadAPI {
    if (!_payCommonUploadAPI) {
        _payCommonUploadAPI = [SAPayCommonUploadAPI apiWithDelegate:self];
    }
    return _payCommonUploadAPI;
}

-(SAPayCommonUploadAPI *)payCommonUploadAPI2 {
    if (!_payCommonUploadAPI2) {
        _payCommonUploadAPI2 = [SAPayCommonUploadAPI apiWithDelegate:self];
    }
    return _payCommonUploadAPI2;
}

-(SAPayRegisterLegalPersonAPI *)payRegisterLegalPersonAPI {
    if (!_payRegisterLegalPersonAPI) {
        _payRegisterLegalPersonAPI = [SAPayRegisterLegalPersonAPI apiWithDelegate:self];
    }
    return _payRegisterLegalPersonAPI;
}

-(SAPayRegisterDetailAPI *)payRegisterDetailAPI {
    if (!_payRegisterDetailAPI) {
        _payRegisterDetailAPI = [SAPayRegisterDetailAPI apiWithDelegate:self];
    }
    return _payRegisterDetailAPI;
}

@end
